package com.itfreer.arcgis.edit;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.itfreer.arcgis.draw.DrawCommand;
import com.itfreer.arcgis.layer.LayerLoader;
import com.itfreer.arcgis.query.GetSelectFeatureListener;
import com.itfreer.arcgis.utils.FeatureTableUtils;
import com.itfreer.arcgis.utils.GeometryUtils;
import com.itfreer.arcgis.utils.LayerUtils;
import com.itfreer.gis.datasyn.entity.ProjectEntity;
import com.itfreer.gis.datasyn.sub.EditLogService;
import com.itfreer.utils.common.ToastUtils;
import com.itfreer.utils.config.ConfigManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class UnionSelectFeatureCommand implements DrawCommand {
    @Override
    public void Execute(Map<String,Object> params)
    {
        FeatureLayer featureLayer = EditToolUtils.getCurrentEditFeatureLayer();
        if(featureLayer == null)
        {
            ToastUtils.showShort(ConfigManager.getContext(),"当前无编辑图层！");
            return;
        }

        if(!FeatureTableUtils.isPolygonFeatuerTable(featureLayer.getFeatureTable()))
        {
            ToastUtils.showLong(ConfigManager.getContext(),"当前编辑图层不是面图层！");
            return;
        }

        //合并选中要素
        LayerUtils.GetSelectFeature(featureLayer, new GetSelectFeatureListener() {
            @Override
            public void execute(FeatureLayer featureLayer, ListenableFuture<FeatureQueryResult> features) {
                final ProjectEntity config = LayerLoader.instance.getConfig(featureLayer);
                try {
                    List<Feature> selectFeatues = new ArrayList<>();
                    for (Feature feature : features.get()) {
                        selectFeatues.add(feature);
                    }
                    if (selectFeatues.size() <= 1) {
                        ToastUtils.showLong(ConfigManager.getContext(),"请选择两个及以上要素进行合并！");
                        return;
                    }
                    List<Geometry> geometries = new ArrayList<>();
                    for (Integer i = 0; i < selectFeatues.size(); i++) {
                        geometries.add(selectFeatues.get(i).getGeometry());
                    }

                    if (!GeometryUtils.CheckGeometryUnionIsSample(geometries)) {
                        ToastUtils.showLong(ConfigManager.getContext(),"选择要素无法完成面合并操作，合并后不是一个单一面要素");
                        return;
                    }

                    // 删除其余选中元素
                    Feature oneFeature = selectFeatues.get(0);
                    oneFeature.setGeometry(GeometryEngine.union(geometries));
                    for (Integer i = 1; i < selectFeatues.size(); i++) {
                        Feature feature = selectFeatues.get(i);
                        Object sguid = feature.getAttributes().get("SGUID");
                        if(sguid!=null){
                            EditLogService.instance.deleteLog(config, sguid.toString());
                        }
                        FeatureTableUtils.DeleteFeature(feature.getFeatureTable(), feature);
                    }

                    // 将合并后的图形，付给第一个要素，并更新
                    // 需改造
                    ValueOption.updateArea(featureLayer, "MIAN_JI", oneFeature.getAttributes(), oneFeature.getGeometry());

                    FeatureTableUtils.UpdateFeature(oneFeature.getFeatureTable(), oneFeature);
                    Object osguid = oneFeature.getAttributes().get("SGUID");
                    if(osguid!=null){
                        EditLogService.instance.editLog(config, osguid.toString());
                    }

                    //去除选中效果
                    featureLayer.unselectFeature(oneFeature);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
